package com.jiayang.hhotel.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.CanRentRoomListAdapter;
import com.jiayang.hhotel.bean.CanRentRoomModel;
import com.jiayang.hhotel.presenter.CanRentRoomPresenter;
import com.jiayang.hhotel.util.DateUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.Calendar;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/17 0017.
 * 可租房页面
 */

public class CanRentRoomActivity extends XActivity<CanRentRoomPresenter> implements View.OnClickListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private ImageView back;//回退
    private ImageView dateLeft;//时间选择左侧按钮
    private ImageView dateRight;//时间选择右侧按钮
    private TextView date;//日期选择控件
    private long currentMillionTime;//当前系统毫秒值
    private String strDate;//标题显示日期字符串
    private String endDate;//请求结束日期

    private TextView one,two,three;//第一·二·三列日期

    private CanRentRoomListAdapter adapter;

    private SwipeMenuRecyclerView recyclerView;
    private SwipeRefreshLayout fresh;

    private Calendar c;

    private DatePicker picker;
    private int page = 1;

    AlertDialog.Builder builder;//日期选择器对话框

    @Override
    public void initData(Bundle savedInstanceState) {

        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        back = (ImageView) findViewById(R.id.iv_back);
        dateLeft = (ImageView) findViewById(R.id.iv_date_left);
        dateRight = (ImageView) findViewById(R.id.iv_date_right);
        date = (TextView) findViewById(R.id.tv_date);
        one = (TextView) findViewById(R.id.tv_date_row_one);
        two = (TextView) findViewById(R.id.tv_date_row_two);
        three = (TextView) findViewById(R.id.tv_date_row_three);
        fresh = (SwipeRefreshLayout) findViewById(R.id.fresh);
        recyclerView = (SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        isFresh(true);
        getP().asyncLoad(strDate,endDate,page);

        setListener();
        currentMillionTime = DateUtil.getCurrentMillionTime();
        strDate = DateUtil.MillionTime2DateTime(currentMillionTime,"yyyy-MM-dd");
        date.setText(strDate+ getString(R.string.three_day));

        setColumnDate();

        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //todo 下拉刷新
                getP().asyncLoad(strDate,endDate,page);
            }
        });

    }

    public void isFresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }

    //设置监听
    private void setListener() {
        back.setOnClickListener(this);
        dateLeft.setOnClickListener(this);
        dateRight.setOnClickListener(this);
        date.setOnClickListener(this);
    }

    /**
     * 设置列标题日期
     */
    private void setColumnDate(){
        c=Calendar.getInstance();
        c.setTimeInMillis(currentMillionTime);
        one.setText(StringUtil.AddZero(c.get(Calendar.MONTH)+ 1) +"-"+StringUtil.AddZero(c.get(Calendar.DATE)));

        c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DATE) + 1);
        two.setText(StringUtil.AddZero(c.get(Calendar.MONTH)+ 1) +"-"+StringUtil.AddZero(c.get(Calendar.DATE)));

        c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DATE) + 1);
        three.setText(StringUtil.AddZero(c.get(Calendar.MONTH)+ 1) +"-"+StringUtil.AddZero(c.get(Calendar.DATE)));
        endDate = c.get(Calendar.YEAR) + "-" + StringUtil.AddZero(c.get(Calendar.MONTH)+ 1) + "-" + StringUtil.AddZero(c.get(Calendar.DATE));
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_can_rent_room;
    }

    @Override
    public CanRentRoomPresenter newP() {
        return new CanRentRoomPresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back://回退
                finish();
                break;
            case R.id.iv_date_left://日期选择左侧
                if(DateUtil.TimeIsLess(strDate,"yyyy-MM-dd")){//当前显示的日期大于系统日期
                    initDate(false);
                    fresh.setVisibility(View.VISIBLE);
                    layout.setVisibility(View.GONE);
                    isFresh(true);
                    getP().asyncLoad(strDate,endDate,page);
                }
                break;
            case R.id.iv_date_right://日期选择右侧
                initDate(true);
                fresh.setVisibility(View.VISIBLE);
                layout.setVisibility(View.GONE);
                isFresh(true);
                getP().asyncLoad(strDate,endDate,page);
                break;
            case R.id.tv_date://日期选择显示控件
                initDatePicker();
                break;
        }
    }

    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        isFresh(false);
            fresh.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(R.mipmap.ic_error);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fresh.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            isFresh(true);
                            getP().asyncLoad(strDate,endDate,page);
                        }
                    });
                    break;
                default://其他错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.net_error));
                    icon.setOnClickListener(null);
                    break;
            }
    }

    private void initDatePicker(){
        View pickerSelect= LayoutInflater.from(this).inflate(R.layout.can_rent_room_picker_select,null);
        picker= (DatePicker) pickerSelect.findViewById(R.id.dp_year);
        picker.init(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),null);
        builder=new AlertDialog.Builder(this);
        builder.setView(pickerSelect);
        builder.setNegativeButton(getString(R.string.determine), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int cuYear,cuMonth,cuDay;
                cuYear=picker.getYear();
                cuMonth=picker.getMonth()+1;
                cuDay=picker.getDayOfMonth();
                String str=cuYear+"-"+StringUtil.AddZero(cuMonth) +"-"+StringUtil.AddZero(cuDay);
                if(DateUtil.TimeIsLess(str,"yyyy-MM-dd")||DateUtil.TimeIsEtc(str,"yyyy-MM-dd")){//选择的日期是未来时间，包括当天
                    strDate = str;
                    currentMillionTime=DateUtil.DateTime2MillionTime("yyyy-MM-dd",strDate);
                    date.setText(strDate+getString(R.string.three_day));
                    setColumnDate();
                }else{
                    ToastUtil.Toast(CanRentRoomActivity.this.getString(R.string.select_time_timeout),CanRentRoomActivity.this);
                }
            }
        });
        builder.create();
        builder.show();
    }

    /**
     * 根据or判断当前显示的日期加一天还是减一天
     * @param or true:加；false:减
     */
    public void initDate(boolean or){
        if(or){//加
            currentMillionTime+=24*60*60*1000*3;
        }else{//减
            currentMillionTime-=24*60*60*1000*3;
        }
        strDate = DateUtil.MillionTime2DateTime(currentMillionTime,"yyyy-MM-dd");
        date.setText(strDate+getString(R.string.three_day));
        setColumnDate();
    }

    public void setAdapter(List<CanRentRoomModel.PForcastRoomDtoListBean> list){
        isFresh(false);
        if(adapter==null){
            adapter=new CanRentRoomListAdapter(this,list);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }
}
